﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.ServiceLocation;
using Subfaculty.Common.Entities;
using Subfaculty.Common.Interfaces.Services;
using Subfaculty.WebApplication.Translators;
using Subfaculty.WebApplication.ViewModels;
using Telerik.Web.Mvc;
using Telerik.Web.Mvc.UI;

namespace Subfaculty.WebApplication.Controllers
{
    public class DisciplineController : Controller
    {
        //
        // GET: /Discipline/
		private readonly IDisciplineRepository _disciplines;
    	private DisciplineTranslator _translators;


    	public DisciplineController()
		{
			_disciplines = ServiceLocator.Current.GetInstance<IDisciplineRepository>();
			_translators = new DisciplineTranslator();
		}

		public ActionResult Index()
		{

			ViewBag.Message = "Список дисциплин";

			var disciplinesPe = _translators.Translate(_disciplines.GetAll());

			return View(disciplinesPe);
		}

		[GridAction]
		public ActionResult _Index()
		{
			Thread.Sleep(2000);
			var disciplinesPe = _translators.Translate(_disciplines.GetAll());

			return View("Index",new GridModel(disciplinesPe));
		}

		[GridAction]
		public ActionResult _SelectAjaxEditing()
		{
			var disciplinesPe = _translators.Translate(_disciplines.GetAll());

			return View(new GridModel(disciplinesPe));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult _SaveAjaxEditing(int id)
		{
			Discipline discipline = _disciplines.Get(id);
			DisciplineViewModel disciplinePe = _translators.Translate(discipline);

			TryUpdateModel(disciplinePe);

			_disciplines.Save(_translators.TranslateBack(disciplinePe, discipline));

			var disciplinesPeList = _translators.Translate(_disciplines.GetAll());
			return View("Index", new GridModel(disciplinesPeList));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult _InsertAjaxEditing()
		{
			DisciplineViewModel disciplinePe = new DisciplineViewModel();
			
			//Perform model binding (fill the product properties and validate it).
			if (TryUpdateModel(disciplinePe))
			{
				Discipline discipline = new Discipline();
				//The model is valid - insert the product.
				_disciplines.Save(_translators.TranslateBack(disciplinePe, discipline));
			}
			//Rebind the grid
			var disciplinesPeList = _translators.Translate(_disciplines.GetAll());
			return View("Index", new GridModel(disciplinesPeList));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult _DeleteAjaxEditing(int id)
		{
			//Find a customer with ProductID equal to the id action parameter
			Discipline discipline = _disciplines.Get(id);
			if (discipline != null)
			{
				//Delete the record 
				_disciplines.Delete(discipline);
			}

			var disciplinesPeList = _translators.Translate(_disciplines.GetAll());
			//Rebind the grid
			return View("Index", new GridModel(disciplinesPeList));
		}

    }
}
